<?php
echo $javascript->link('ui/ui.core.js', false);
echo $javascript->link('ui/ui.draggable.js', false);
echo $javascript->link('ui/ui.droppable.js', false);
echo $javascript->link('ui/effects.core.js', false);
echo $javascript->link('ui/effects.highlight.js', false);

$total = $type_stats = $graph->stats($workouts, 'total');
$total_h_m = $timeUtility->minuteToHourMinute($total['Total'][0]['time']);
?>
<div class="bar">
	<ul class="subnav">
		<li><a href="#">Workout templates</a></li>
		<li><a href="#">Define types</a></li>
	</ul>
	<ul class="btn-bar">
		<li><?php echo $html->link(__('Today', true), array('action' => 'month'), array('class'=>"btn-plain")); ?></li>
		<li><?php echo $html->link(__('Today', true), array('action' => 'month'), array('class'=>"btn-plain")); ?></li>
	</ul>
	<ul class="btn-bar btn-bar-grouped">
		<li><?php echo $html->link(__('Today', true), array('action' => 'month'), array('class'=>"btn-plain active")); ?></li>
		<li><?php echo $html->link(__('Today', true), array('action' => 'month'), array('class'=>"btn-plain")); ?></li>
	</ul>
</div>

<div class="content-holder-1">
	
	<div class="holder">
		<div class="main">
			<div class="top-bar-2 rounded-top pagina">
				<?php echo $html->link('&#9664;', array('action' => 'month', $year, $month-1), array('class'=>'prev left', 'title'=>__('Previous', true)), null, false); ?>
				<?php echo $html->link('&#9654;', array('action' => 'month', $year, $month+1),array('class'=>'next right', 'title'=>__('Next', true)), null, false); ?>
				<?php if(isset($total_h_m)):?>
				<span>
					
				<?php echo $header;?>: <?php echo $total_h_m[0]?> <?php __('hours and') ?>  <?php echo $total_h_m[1]?> <?php __('min of training') ?></span>
				<?php else: ?>
					<?php echo date('F, Y', mktime(0, 0, 0, $month, 1, $year))?>
				<?php endif; ?>
			</div>
			<?php //debug($total['Total'][0]['time'])?>
			
			<ul class="calendar">
				<?php foreach($weekdays as $num => $day):?>
				<li class="<?php echo $day['class']; ?> day<?php echo $num; ?>">
					<div class="day-holder">
						<?php echo $day['short']; ?>
					</div>
				</li>
				<?php endforeach; ?>
				<?php $num = 0; ?>
				<?php foreach($days as $day):?>
					<?php 
					if (($num%7)==0):?>
					<?php $weeknum = $day['week_num']; ?>
					<li class="week">
						<div class="week-title"><?php __('Week')?> <?php echo $weeknum;?> 
							
							<?php if (isset($day['Period'])): ?>
								- <?php echo $html->link($day['Period']['Period']['title'], array('controller'=>'periods','action' => 'view',$day['Period']['Period']['id']), array('class'=>'nyroModal')); ?> 
							<?php endif; ?>
							</div>
							<?php if (isset($day['Status'])): ?>
								<?php echo $html->link(__('View summary', true), array('controller'=>'statuses','action' => 'view',$day['Status']['Status']['id']), array('class'=>'nyroModal')); ?> 
								
							<?php else: ?>
								<?php echo $html->link(__('Add summary', true), array('controller'=>'statuses','action' => 'add',$day['date']), array('class'=>'nyroModalx')); ?>
							<?php endif; ?>
							
							<?php echo $html->link(__('View graph', true), array('controller' => 'graph', 'action' => 'week', $year, $weeknum), array('class'=>'btn-3 nyroModal')); ?>
							
							
						<div class="right">
						<label for="week_copy_<?php echo $weeknum; ?>">Copy workouts to</label>
						<select class="week_copy" id="week_copy_<?php echo $weeknum; ?>">
							<?php for($j=4; $j >=-4; $j--):?>
								<?php $copy_string = $day['date'] . '/' . $j; ?>
								<?php if ($j > 0): ?>
								<option value="<?php echo $copy_string;?>"><?php echo abs($j);?> week ahead</option>
								<?php elseif ($j < 0): ?>
									<option value="<?php echo $copy_string;?>"><?php echo $j;?> week before</option>
								<?php else: ?>
									<option value="null" selected="selected">Pick a week</option>
								<?php endif; ?>
							<?php endfor;?>
						</select>
						</div>
					</li>
					<?php endif; $num++;?>
					<li class="<?php echo $day['class']; ?> day<?php echo $day['day_num']?>">
						<div class="day-holder" title="<?php echo $day['date']; ?>">
							<span class="day_num"><?php echo $day['short']; ?> </span>
							<?php echo $html->link(__('Add', true), array('action' => 'add', $day['date']), array('class'=>'add nyroModal')); ?>
							
							<div class="event-holder" id="event-holder-<?php echo $day['date']; ?>">
							<?php if(isset($day['Workouts'])):?>
								<?php echo $this->element('../workouts/elements/day', array( 'workouts'=>$day['Workouts'])); ?>
								
							<?php endif; ?>
							</div>
						</div>
					</li>

				<?php endforeach; ?>

			</ul> 
			<script type="text/javascript">
				jQuery(document).ready(function(){
					
					jQuery("a.nyroModalLive").live("click", function(){
						jQuery(this).nyroModalManual();
						//alert(jQuery(this));
						return false;
					});
					
					// Drag and move workouts inside calandar
					jQuery('.workout-event').live("mouseover", function() { 
						//alert('tests');
						if (!$(this).data("init")) { 
							$(this).data("init", true); 
							jQuery(this).draggable({
								cancel: 'a.ui-icon',
								revert: 'invalid',
								containment: 'ul.calendar',
								helper: 'clone',
								cursor: 'move'
							});
						}
					});
					
					jQuery('.day-holder').droppable({
						accept: '.workout-event',
						activeClass: 'ui-state-highlight',
						drop: function(ev, ui) {
							droppedWorkout(ui.draggable,$(this));
						}
					});
					
					var url = "<?php echo $html->url(array('action'=>'move'))?>";
					function droppedWorkout(item, target) {
						var query = url + '/' + item.attr('rel') + '/' + target.attr('title');
						
						jQuery.getJSON(query, function(data){
							item.hide();
							item.appendTo(target.find('.event-holder')).fadeIn();
						});
					}
					
					// Copy one week of workouts to another
					var copy_url = "<?php echo $html->url(array('action'=>'copyweek'))?>";
					jQuery("select.week_copy").change(function() {
						var val = jQuery(this).val();
						//alert(val);
						if(val != "null") {
							window.location = copy_url + '/' + val;
							//alert(val);
						}
						
					});
					
				});
			</script>
			
		</div><!--End .main -->
		<div class="sidebar">
			<?php 
			if(isset($user)) {
				echo $this->element('sidebar/user', array('user'=>$user));
			}
			if(isset($templates)) {
				echo $this->element('sidebar/templates', array('templates'=>$templates));
			}
			if(isset($stats)) {
				echo $this->element('sidebar/stats', array('stats'=>$stats));
			}
			?>
			
		</div><!--End .sidebar -->
	</div><!--End .holder -->
</div>	
